﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using ZXing.QrCode;
using ZXing;
using ZXing.Common;
using ZXing.QrCode.Internal;
namespace RookieLibSharp
{
    [Flags]
    public enum RectanglePositon
    {
        TopLeft = 0x01,
        TopRight = 0x02,
        BottomRight = 0x04,
        BottomLeft = 0x08,
        ALL = 0x0F
    }

    public class QRCode
    {
        public static Bitmap CreateQRCode(string content)
        {
            return CreateQRCode(content,100);
        }

        public static Bitmap CreateQRCode(string content, int size , Bitmap centerImg = null)
        {
            BarcodeWriter barcodeWriter = new BarcodeWriter();
            EncodingOptions encodingOptions = new EncodingOptions();
            IDictionary<EncodeHintType, object> Hint = encodingOptions.Hints;

            encodingOptions.Width = size;
            encodingOptions.Height = size;
            encodingOptions.Margin = 0;
            Hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

            barcodeWriter.Format = BarcodeFormat.QR_CODE;
            barcodeWriter.Options = encodingOptions;
            Bitmap image = barcodeWriter.Write(content);
            if (centerImg != null)
            {
                if (centerImg.Width != 65)
                {
                    centerImg = (Bitmap)ResizeImage(centerImg, 65, 65);
                }
                image = (Bitmap)CombinImage(image, centerImg);
            }
            return image;
        }
        public static string DecodeQRCode(Bitmap qrCode)
        {
            BarcodeReader barcodeReader = new BarcodeReader();
            string content = barcodeReader.Decode(qrCode).ToString();
            return content;
        }
        private static Image ResizeImage(Image srcImage,int width,int height)
        {
            try  
            {
                Image dstImage = new Bitmap(width, height);
                Graphics g = Graphics.FromImage(dstImage);  
                // 插值算法的质量    
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(srcImage, new Rectangle(0, 0, width, height), new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);  
                g.Dispose();
                return dstImage;  
            }  
            catch  
            {  
                return null;  
            } 
        }
        private static Image CombinImage(Image backImage, Image image)
        {
            Graphics g = Graphics.FromImage(backImage);
            g.DrawImage(backImage, 0, 0, backImage.Width, backImage.Height);
            g.DrawImage(image, backImage.Width / 2 - image.Width / 2, backImage.Width / 2 - image.Width / 2, image.Width, image.Height);
            g.Dispose();
            GC.Collect();
            return backImage;
        }
        public static Image RoundedCorner(Image image, int radius, RectanglePositon rPos)
        {
            Bitmap bitMap = new Bitmap(image.Width, image.Height);
            Graphics gBitMap = Graphics.FromImage(bitMap);
            gBitMap.SmoothingMode = SmoothingMode.HighQuality;
            gBitMap.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gBitMap.CompositingQuality = CompositingQuality.HighQuality;
            



            Graphics g = Graphics.FromImage(image);
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;
            Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
            GraphicsPath rectPath = new GraphicsPath();
            rectPath.AddEllipse(0, 0, image.Width, image.Height);
            //GraphicsPath rectPath = CreateRoundRectanglePath(rect, radius, rPos); //构建圆角外部路径
            Brush b = new SolidBrush(Color.White);//圆角背景白色
            g.DrawPath(new Pen(b), rectPath);
            g.FillPath(b, rectPath);
            g.Dispose();
            return image;
        }
        private static GraphicsPath CreateRoundRectanglePath(Rectangle rect, int radius, RectanglePositon rPos)
        {
            GraphicsPath rectPath = new GraphicsPath();
            if ((rPos & RectanglePositon.TopLeft) == RectanglePositon.TopLeft)
            {
                rectPath.AddArc(rect.Left, rect.Top, radius * 2, radius * 2, 180, 90);
                rectPath.AddLine(rect.Left, rect.Top, rect.Left, rect.Top + radius);
            }
            if ((rPos & RectanglePositon.TopRight) == RectanglePositon.TopRight)
            {
                rectPath.AddArc(rect.Right - radius * 2, rect.Top, radius * 2, radius * 2, 270, 90);
                rectPath.AddLine(rect.Right, rect.Top, rect.Right - radius, rect.Top);
            }
            if ((rPos & RectanglePositon.BottomLeft) == RectanglePositon.BottomLeft)
            {
                rectPath.AddArc(rect.Left, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90);
                rectPath.AddLine(rect.Left, rect.Bottom - radius, rect.Left, rect.Bottom);
            }
            if ((rPos & RectanglePositon.BottomRight) == RectanglePositon.BottomRight)
            {
                rectPath.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90);
                rectPath.AddLine(rect.Right - radius, rect.Bottom, rect.Right, rect.Bottom);
            }
            return rectPath;
        }
    }
}
